<?php
/**
 * debug
*/
define("XPFACE_DEBUG", true);
require_once('pworks/mvc/action/BaseAction.class.php');
require_once "xpface/util/XPFaceConfigLoader.class.php";
require_once "xpface/util/XPFace.class.php";
require_once "xpface/util/data/DataSourceFactory.class.php";

class XPFaceDebugAction extends BaseAction{
    
    /**
	 * @isnull false
	 */
    public $template;
    public $modules;
    
    public function execute(){
        //xpface config
        $xpFaceConfigLoader = XPFaceConfigLoader::getInstance();
        $xpFaceConfig = $xpFaceConfigLoader->getXPFaceConfig();
        //var_dump($xpFaceConfig);
        
        //init tpl file
        $pythonCommand = $xpFaceConfig["python_command"]->value;
        $parseScript = $xpFaceConfig["parse_script"]->value;
        $sourceTplPath = $xpFaceConfig["source_tpl_path"]->value;
        $parsedTplPath = $xpFaceConfig["parsed_tpl_path"]->value;
        
        
        $modulesDir = dirname($this->modules);
        $moduleNames = basename($this->modules);
        $moduleNamesArr = explode(",", $moduleNames);
        
        //parse tpl file
        $currentPageConfig = new PageConfig();
        $currentPageConfig->id = "debug_page_id";
        $currentPageConfig->template = $this->template . ".tpl";
        $tmpRegions = array();
        foreach ($moduleNamesArr as $moduleName) {
            $moduleNameArr = explode(":", $moduleName);
            $tmpRegions[$moduleNameArr[0]] = $modulesDir . "/" . $moduleNameArr[1] . ".tpl";
        }
        $currentPageConfig->regions = $tmpRegions;
        //var_dump($currentPageConfig);exit;
        XPFace::$_config = $currentPageConfig;
        XPFace::setAction($this);

        //template
        $sourceTplFile = $sourceTplPath . "/" . $currentPageConfig->template;
        $copiedSourceFile = $parsedTplPath . "/" . $currentPageConfig->template;
        $parsedTplFile = $parsedTplPath . "/" . $currentPageConfig->template . ".php";
        if (!file_exists($copiedSourceFile) || md5_file($copiedSourceFile) != md5_file($sourceTplFile)) {
            if (!file_exists($sourceTplFile)) {
                echo "File '{$parsedTplFile}' parse failing! ";
                exit;
            }
            copy($sourceTplFile, $copiedSourceFile);
            if (file_exists($parsedTplFile)) {
                unlink($parsedTplFile);
            }
            //var_dump("python D:/python/system/ParseTags.py {$sourceTplFile} {$parsedTplFile}");
            $cmd = "{$pythonCommand} {$parseScript} {$sourceTplFile} {$parsedTplFile}";
            exec($cmd);
            if (!file_exists($parsedTplFile)) {
                echo "File '{$parsedTplFile}' parse failing! ";
                exit ;
            }
        }
        foreach ($currentPageConfig->regions as $regionId => $sourceUIModule) {
            $sourceTplFile = $sourceTplPath . "/" . $sourceUIModule;
            $copiedSourceFile = $parsedTplPath . "/" . $sourceUIModule;
            $parsedTplFile = $parsedTplPath . "/" . $sourceUIModule . ".php";
            if (file_exists($sourceTplFile) && file_exists($copiedSourceFile) && md5_file($copiedSourceFile) == md5_file($sourceTplFile)) {
                continue;
            }
            copy($sourceTplFile, $copiedSourceFile);
            if (file_exists($parsedTplFile)) {
                unlink($parsedTplFile);
            }
            //var_dump("python D:/python/system/ParseTags.py {$sourceTplFile} {$parsedTplFile}");
            $cmd = "{$pythonCommand} {$parseScript} {$sourceTplFile} {$parsedTplFile}";
            //var_dump($cmd);
            exec($cmd);
            if (!file_exists($parsedTplFile)) {
                echo "File '{$parsedTplFile}' parse failing! ";
                exit;
            }
        }
        //datasource
        
        $binds = array();
        foreach ($currentPageConfig->binds as $bindKey => $bindValue) {
            //var_dump($bindKey, $bindValue);
            $binds[$bindKey] = $bindValue;
        }
        $this->setData("binds", $binds);
        foreach ($currentPageConfig->regions as $regionId => $uimoduleSrc) {
            $dataSource = $xpFaceConfigLoader->getDataSourceConfig($uimoduleSrc);
            //var_dump($dataSource);
            if ($dataSource === null) {
                continue;
            }
            //var_dump($dataSource->data);
            $data = array();
            foreach ($dataSource->data as $key => $source) {
                $data[$key] = $source;
            }
            $this->setData(md5($dataSource->uimodule), $data);
        }
        include($xpFaceConfig["parsed_tpl_path"]->value."/".$currentPageConfig->template.".php");
    }
}
